<?php

namespace App\Excels\Imports;

use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithMapping;

class JinShouFuSubAccountImport implements ToArray, WithMapping
{

    public array $datas = [
        "date"=>"",
        "datas"=>[]
    ];

    public function map($row): array
    {
        return $row;
    }

    public function array(array $array): void
    {
        if (empty($array)) return;
        array_shift($array);
        $pattern = '/起始日期:(\d{4}-\d{2}-\d{2})/';
        preg_match($pattern, $array[0][0], $matches);
        if(empty($matches[1])) return;
        $this->datas["date"] = $matches[1];
        array_shift($array);
        array_shift($array);
        array_shift($array);
        array_shift($array);
        foreach ($array as $value){
            if(str_contains($value[0],"共"))continue;
            $this->datas["datas"][] = [
                "merchantName"=>trim($value[5]),
                "tradeNo"=>$value[1],
                "totalMoney"=>$value[6],
                "fee"=>$value[7],
                "cutFee"=>$value[8],
                "payment"=>$value[9],
                "payType"=>$value[4],
                "createdAt"=>$value[1],
            ];
        }
    }
}
